
const md5 = require('md5');
const {pay_api_key} = require('../config.js');
module.exports = {
    log(value){
        console.log(value);
    },
    genOrderId(){
        var randomNumber = ""; //订单号
        for (var i = 0; i < 6; i++) { //6位随机数，用以加在时间戳后面。
        
            randomNumber += Math.floor(Math.random() * 10);
        }
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();

        const formatNumber = n => {
            n = n.toString()
            return n[1] ? n : '0' + n
        }

        return [year, month, day, hour, minute, second].map(formatNumber).join('') + randomNumber;
    },
    
    getNonceStr(len){
            let str = "0123456789abcdefghijklmnopqrstuvwxyz";
            let newStr = "";
            for(let i=0; i<len; i++){
                let index = Math.floor(Math.random() * str.length);
                newStr += str[index];
            }
            return newStr;
    },


    getOrder() {
        let date = new Date();
        const year = date.getFullYear()
        const month = date.getMonth() + 1
        const day = date.getDate()
        const hour = date.getHours()
        const minute = date.getMinutes()
        const second = date.getSeconds()
    
        const formatNumber = n => {
            n = n.toString()
            return n[1] ? n : '0' + n
        }
        return [year, month, day, hour, minute, second].map(formatNumber).join('');
    },
    // 下单数据进行数据签名（加密）
    createSign(obj) {
        //签名算法（把所有的非空的参数，按字典顺序ASCILL组合起来+key,然后md5加密，再把加密结果都转成大写的即可）
        let stringA = 'appid=' + obj.appid + '&body=' + obj.body + '&mch_id=' + obj.mch_id + '&nonce_str=' + obj.nonce_str + '&notify_url=' + obj.notify_url + '&openid=' + obj.openid + '&out_trade_no=' + obj.out_trade_no + '&spbill_create_ip=' + obj.spbill_create_ip + '&total_fee=' + obj.total_fee + '&trade_type=' + obj.trade_type;
        let stringSignTemp = stringA + '&key=' + pay_api_key;
        stringSignTemp = md5(stringSignTemp);
        let signValue = stringSignTemp.toUpperCase();
        return signValue
    }
}